//
//  QPMapViewController.m
//  A_project2.0
//
//  Created by Tiny on 16/3/15.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "QPMapViewController.h"
#import <MapKit/MapKit.h>

@interface QPMapViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
{
    MKMapView *mapView;
    CLLocationManager *locationManager;
    
    UIButton *_locationButton;  /**< 定位 */
}
@end

@implementation QPMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];//导航条返回键带的首页 让他消失

    
    mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:mapView];
    // 设置地图默认区域
    [mapView setRegion:MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(self.latStr, self.lngStr), 5000, 5000) animated:YES];
    mapView.delegate = self;

    
    // 显示3D地图
    // Create a coordinate structure for the location.
    CLLocationCoordinate2D ground = CLLocationCoordinate2DMake([self.latStr doubleValue], [self.lngStr doubleValue]);
    // Create a coordinate structure for the point on the ground from which to view the location.
    CLLocationCoordinate2D eye = CLLocationCoordinate2DMake([self.latStr doubleValue], [self.lngStr doubleValue]);
    // Ask Map Kit for a camera that looks at the location from an altitude of 100 meters above the eye coordinates.
    MKMapCamera *myCamera = [MKMapCamera cameraLookingAtCenterCoordinate:ground fromEyeCoordinate:eye eyeAltitude:100];
    // Assign the camera to your map view
    mapView.camera = myCamera;
    //.滑动和缩放地图内容
    CLLocationCoordinate2D mapCenter = mapView.centerCoordinate;
    mapCenter = [mapView convertPoint:
                 CGPointMake(1, (mapView.frame.size.width/2.0))
                 toCoordinateFromView:mapView];
    [mapView setCenterCoordinate:mapCenter animated:YES];
    MKCoordinateRegion theRegion = mapView.region;
    
    // Zoom out
    theRegion.span.longitudeDelta *= 2.0;
    theRegion.span.latitudeDelta *= 2.0;
    [mapView setRegion:theRegion animated:YES];
    
    
    //在Info.plist右键，Add Row,添加Key为NSLocationAlwaysUsageDescription
    mapView.showsUserLocation = YES;
    
    
    // 添加按键 跳转系统地图
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"跳至系统" style:UIBarButtonItemStylePlain target:self action:@selector(tiaozhuan)];
    [self initControls];
}
- (void) initControls
{
    _locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _locationButton.frame = CGRectMake(20, CGRectGetHeight(mapView.bounds) - 80, 40, 40);
    _locationButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    _locationButton.backgroundColor = [UIColor whiteColor];
    _locationButton.layer.cornerRadius = 5;
    [_locationButton addTarget:self action:@selector(locateAction) forControlEvents:UIControlEventTouchUpInside];
    [_locationButton setImage:[UIImage imageNamed:@"no-dingwei"] forState:UIControlStateNormal];
    [mapView addSubview:_locationButton];
}
- (void)locateAction
{
    if (mapView.userTrackingMode != MKUserTrackingModeFollow) {
        [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
    }
}
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated
{
    // 修改定位
    if (mode == MKUserTrackingModeNone) {
        [_locationButton setImage:[UIImage imageNamed:@"no-dingwei"] forState:UIControlStateNormal];
    }else{
        [_locationButton setImage:[UIImage imageNamed:@"yes-dingwei"] forState:UIControlStateNormal];
    }

}

- (void)tiaozhuan
{
    self.hidesBottomBarWhenPushed = YES;
    MapViewController *p = [[MapViewController alloc]init];
    p.str = self.nameStr;
    [self.navigationController pushViewController:p animated:YES];

}



//- (void)initLocation
//{
//    // Create the location manager if this object does not
//    // already have one.
//    if (nil == locationManager)
//        locationManager = [[CLLocationManager alloc] init];
//    locationManager.delegate = self;
//    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
//        [locationManager requestWhenInUseAuthorization];
//    }
//    if(![CLLocationManager locationServicesEnabled]){
//        NSLog(@"请开启定位:设置 > 隐私 > 位置 > 定位服务");
//    }
//    if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
//        [locationManager requestAlwaysAuthorization]; // 永久授权
//        [locationManager requestWhenInUseAuthorization]; //使用中授权
//    }
//    [locationManager startUpdatingLocation];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
